package org.nkumar.mesex;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

public final class MesExRuleMetadata
{
    //class name of relevant variable
    private final String fullClassName;
    private final String className;

    //the index of the param that holds the key for a given method
    private final Map<String, Integer> keyParamIndices;

    private final String methodCallPattern;


    public MesExRuleMetadata(final String className, final String fullClassName,
            final Map<String, Integer> keyParamIndices)
    {
        this.fullClassName = className;
        this.className = fullClassName;
        this.keyParamIndices = Collections.unmodifiableMap(
                new HashMap<String, Integer>(keyParamIndices));
        this.methodCallPattern = initMethodCallPattern();
    }

    public String getFullClassName()
    {
        return fullClassName;
    }

    public String getClassName()
    {
        return className;
    }

    public Map<String, Integer> getKeyParamIndices()
    {
        return keyParamIndices;
    }

    public int getKeyParamIndexForMethod(String methodName)
    {
        final Integer integer = keyParamIndices.get(methodName);
        if (integer == null)
        {
            return -1;
        }
        return integer;
    }

    public String getMethodCallPattern()
    {
        return methodCallPattern;
    }

    private String initMethodCallPattern()
    {
        final StringBuilder buffer = new StringBuilder(100);
        buffer.append("(");
        boolean firstAppended = false;
        for (final String methodName : keyParamIndices.keySet())
        {
            if (firstAppended)
            {
                buffer.append("|");
            }
            else
            {
                firstAppended = true;
            }
            buffer.append(methodName);
        }
        buffer.append(")");
        return buffer.toString();
    }

}
